package com.googlecode.imanager.client.style;

import java.io.Serializable;

import com.googlecode.imanager.client.bridge.style.IBorder;
import com.googlecode.imanager.client.util.Utils;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class Border implements IBorder, Serializable {

    private static final long serialVersionUID = 1L;
    
    public static final Border EMPTY_BORDER = new Border(0);
    
    private Color color;
    private Integer width;
    private BorderStyle style;
    
    public Border(Color color) {
        super();
        this.color = color;
    }

    public Border(Integer width) {
        super();
        this.setWidth(width);
    }

    public Border(BorderStyle style) {
        super();
        this.style = style;
    }

    public Border(Color color, Integer width) {
        super();
        this.color = color;
        this.setWidth(width);
    }

    public Border(Color color, BorderStyle style) {
        super();
        this.color = color;
        this.style = style;
    }
    
    public Color getColor() {
        return color;
    }
    
    private void setWidth(Integer width){
        if (width != null
                && width < 0){
            width = 0;
        }
        this.width = width;
    }

    public Integer getWidth() {
        return width;
    }
    
    public BorderStyle getStyle() {
        return style;
    }
    
    public final int getInset(){
        return width != null ? width.intValue()
             : color != null ? 1
             : style != null ? 1 : 0;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this){
            return true;
        }
        if (obj instanceof Border){
            Border b = (Border) obj;
            return Utils.isEquals(color, b.color)
                && Utils.isEquals(width, b.width)
                && Utils.isEquals(style, b.style);
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        
        if (color != null){
            hash = color.hashCode();
        }
        if (width != null){
            hash ^= width.hashCode() << 24;
        }
        if (style != null){
            hash ^= style.hashCode();
        }
        
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder value = new StringBuilder();
        value.append("Border[");
        boolean next = false;
        
        if (color != null){
            next = true;
            value.append("color=").append(color);
        }
        if (width != null){
            if (next){
                value.append(", ");
            } else {
                next = true;
            }
            value.append("width=").append(width);
        }
        if (style != null){
            if (next){
                value.append(", ");
            }
            value.append("style=").append(style);
        }
        
        value.append(']');
        return value.toString();
    }
    
}
